new Vue({
    el: '#app',
    data() {
        return {
            pickerOptions: pickerOptions,
            form: {
                object_id: '201922222',
                object_type: "contract",
                enterprise_info_serialnumber: $.cookie('enterprise'),
                approval_info_id: "",
                founder: $.cookie('use'),
            },
            visibleDialog: false,
            Node_id: null,//选中
            nodeUserList: [],//弹窗可选用户
            data: [],
            loading: false,
            isWindow: window.innerWidth < 768 ? true : false
        }
    },
    mounted() {
        window.addEventListener('resize', () => {
            this.isWindow = window.innerWidth < 768 ? true : false
        });
        this.getData();
    },
    computed: {
        icon() {
            return function (type) {
                if (type == "1") {
                    return "el-icon-s-check"
                } else if (type == "2") {
                    return "el-icon-s-promotion"
                } else {
                    return ""
                }
            }
        },
        isMark() {
            return function (item) {
                if (item.settype == '1') {//1指定成员 
                    try {
                        return JSON.parse(item.Current_Node_Approver).nodeUserList.length > 1
                    } catch (error) { }
                } else if (item.settype == '2' && item.selectMode == '2') {//2发起人自选
                    return true
                }
                return false
            }
        },
        isAdd() {
            return function (item) {
                if (item.selectMode == '1') {//选择1个
                    return item.userlist.length == 0
                } else if (item.selectMode == '2') {//选择多个
                    let bln = false;
                    try {
                        bln = JSON.parse(item.Current_Node_Approver).nodeUserList.length > item.userlist.length
                    } catch (error) { }
                    return bln
                }
                return false
            }
        },
        mark() {
            return function (type) {
                if (type == "1") {
                    return "会审"
                } else if (type == "2") {
                    return "或审"
                } else if (type == "3") {
                    return "依次审批"
                }
            }
        }
    },
    methods: {
        getData() {
            this.getFunctionApproval('contract', (approval_info_id) => {
                this.getNewApprova(approval_info_id);
            })
        },
        getFunctionApproval(Function_Approval_Code, callback) {
            const _this = this;
            $.ajax({
                url: DomainName + '/getFunctionApprovalAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Enterprise_Info_SerialNumber: $.cookie('enterprise'),
                    Function_Approval_Code,
                },
                success: function (res) {
                    if (res.Status == 'success' && res.Data.length > 0) {
                        _this.form.approval_info_id = res.Data[0].Approval_Info_SerialNumber
                        callback(res.Data[0].Approval_Info_SerialNumber)
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        getNewApprova(approval_info_id) {
            const _this = this;
            $.ajax({
                headers,
                url: BASE_URL + '/Flow/getNewApprova',
                type: 'POST',
                data: {
                    approval_info_id,
                    founder: $.cookie('use'),
                    object_id: '201922222',
                    object_type: 'contract'
                },
                success: function (res) {
                    console.log(res);
                    if (res.status == 0) {
                        res.data.forEach(item => {
                            item.userlist = [];
                            if (item.settype == '1' || item.Current_Node_Approver_Type == '2') {
                                let arr;
                                try {
                                    arr = JSON.parse(item.Current_Node_Approver).nodeUserList
                                } catch (error) { }
                                arr.forEach(ite => {
                                    item.userlist.push(ite)
                                })
                            }
                        })
                        console.log(res.data);
                        _this.data = res.data
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        config(node) {//父节点子节点添加
            let group_node = '';
            let newNode = []
            for (let i = 0; i < node.length; i++) {
                //关联父子关系
                let obj = deepClone(node[i]);
                obj.Group_Node = [group_node]
                if (node[i + 1]) {
                    obj.Children_Node = [node[i + 1].Node_id]
                } else {
                    obj.Children_Node = []
                }
                group_node = node[i].Node_id
                //
                let Current_Node_Approver = {userlist:[]}
                obj.userlist.forEach(item=>{
                    Current_Node_Approver.userlist.push(item.targetId);
                })
                obj.Current_Node_Approver = JSON.stringify(Current_Node_Approver);
                delete obj.userlist;
                newNode.push(obj)
            }
            return newNode
        },
        Add(item) {//添加
            this.visibleDialog = true;
            try {
                let nodeUserList = JSON.parse(item.Current_Node_Approver).nodeUserList
                let userlist = item.userlist
                nodeUserList.forEach(i => {
                    i.check = userlist.some(j => j.targetId == i.targetId)
                })
                this.nodeUserList = nodeUserList
            } catch (error) { }
            this.Node_id = item.Node_id
        },
        Delete(item, ite) {//删除
            item.userlist = item.userlist.filter(i => i.targetId != ite.targetId)
        },
        Select(item) {//选择弹窗
            let obj = this.data.filter(i => i.Node_id == this.Node_id)[0];
            if (!obj.userlist.some(i => i.targetId == item.targetId)) {//已选中的不重复填
                obj.userlist.push(item);
            }
            this.visibleDialog = false;
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    _this.form.arlist = _this.config(_this.data);
                    $.ajax({
                        headers: {
                            ...headers,
                            'Content-Type': 'application/json;charset=UTF-8'
                        },
                        url: BASE_URL + '/Flow/addNewApprova',
                        type: 'POST',
                        data: JSON.stringify(_this.form),
                        success: function (res) {
                            if (res.status == 0) {
                                _this.submit(res.data);
                            } else {
                                _this.$message.error("操作失败，请稍后再试")
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        submit(data) {//首次申请提交
            const _this = this;
            console.log(JSON.stringify({
                "userId": $.cookie('use'),
                "approver_opinions": "首次提交",
                "approver_result": "1", //1 同意 2.拒绝 3.移交
                "founder": [], //如果移交选移交人
                "flow_id": data.Approval_Record_SerialNumber, //整个流程Approval_Record_SerialNumber
                "onApproval_record_detail_id": data.Approval_Record_Detail_SerialNumber //当前节点Approval_Record_Detail_SerialNumber
            }));
            $.ajax({
                headers: {
                    ...headers,
                    'Content-Type': 'application/json;charset=UTF-8'
                },
                url: BASE_URL + '/Flow/commitApprova',
                type: 'POST',
                data: JSON.stringify({
                    "userId": $.cookie('use'),
                    "approver_opinions": "首次提交",
                    "approver_result": "1", //1 同意 2.拒绝 3.移交
                    "founder": [], //如果移交选移交人
                    "flow_id": data.Approval_Record_SerialNumber, //整个流程Approval_Record_SerialNumber
                    "onApproval_record_detail_id": data.Approval_Record_Detail_SerialNumber //当前节点Approval_Record_Detail_SerialNumber
                }),
                success: function (res) {
                    console.log(res);
                    if (res.status == 0) {
                        _this.$message.success(res.msg)
                        window.history.go(-1);
                    } else {
                        _this.$message.error("操作失败，请稍后再试")
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    _this.loading = false;
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        deal() {
            const modify = deepClone(this.form)
            return { add: null, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})